package com.mjw.zstu.untils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

public class Zxing {
    public static String zxing(int exhibition_id,int user_id){
        int width=300;
        int height=300;
        long t=System.currentTimeMillis();
        String format=".png";
        String content=exhibition_id+","+user_id;
        String path="/Users/xiaoming/Pictures/image/"+String.valueOf(t)+format;
        HashMap hints =new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN,2);
        try {
            BitMatrix bitMatrix =new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
            Path file=new File(path).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix,"png",file);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "http://localhost:3001/image/"+String.valueOf(t)+format;
    }
}
